var xmlhttp;
var map;
var geocoder;
var address;

function initialize() {	
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(23.704894502324912, 90.3955078125), 6);
    map.setUIToDefault();
    map.setMapType(G_HYBRID_MAP);        
    geocoder = new GClientGeocoder();
  }
  GEvent.addListener(map, "click", function(overlay, point) {	  
    var lat = point.lat();
    var lon = point.lng();
    getImages(lat, lon);
    address = point;
    geocoder.getLocations(point, showAddress);
  });
  var i_lat = 23.7048;
  var i_lon = 90.3955;
  getImages(i_lat, i_lon);
  var init_point = new GLatLng(i_lat, i_lon);
  geocoder.getLocations(init_point, showAddress);  
}

function getImages(lat, lon){	
  document.getElementById("popUpDiv").style.visibility = "hidden";
  document.getElementById("image_canvas").innerHTML = "<img src=\"loading.gif\"></img>";
  if(xmlhttp == null){
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
	
  var url = "earthalbumext?lat=" + lat + "&lon=" + lon;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange = showImages;
  xmlhttp.send(null);
}

function showImages(){	
  if (xmlhttp.readyState==4) {
    if (xmlhttp.status==200) {
      var photos = xmlhttp.responseXML.getElementsByTagName('photo');
      document.getElementById("image_canvas").innerHTML = "";
      if(photos.length > 0){
        var farm_id, server_id, photo_id, secret, size;
        var image_tag, image_src, image_src_m;
        for (var i=0; i < photos.length; i++){
          farm_id = photos[i].getAttribute('farm');
          server_id = photos[i].getAttribute('server');
          photo_id = photos[i].getAttribute('id');
          secret = photos[i].getAttribute('secret');
          size = 's';
          image_src_m = "http://farm" + farm_id + ".static.flickr.com/" + server_id + "/" + photo_id + "_" + secret + "_m.jpg";
          image_src = "http://farm" + farm_id + ".static.flickr.com/" + server_id + "/" + photo_id + "_" + secret + "_" + size + ".jpg";
          image_tag = "<img src=\"" + image_src + "\" onclick=\"showMediumImage('" + image_src_m + "','" + photo_id + "','" + secret + "');\" style=\"cursor: hand; cursor: pointer\"></img>";
          document.getElementById("image_canvas").innerHTML += image_tag;
        }
      }
      else{
        document.getElementById("image_canvas").innerHTML = "No images found for this location!";
      }
    }
  }
}

function showMediumImage(url, photo_id, secret){	
  var req;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
	
  var nurl = "getInfo?photo_id=" + photo_id + "&secret=" + secret;
	
  req.open("GET",nurl,false);
  req.send(null);
  var rXML = xmlhttp.responseXML;
    
  var title = rXML.getElementsByTagName("title")[0].childNodes[0].nodeValue;
  var description = rXML.getElementsByTagName("description")[0].childNodes[0].nodeValue;
    	        
  var img = "<img src=\"" + url+ "\"></img>";
	    
  var table = "<table bgcolor=\"#eeeeff\" border=\"1\" cellpadding=\"4\" cellspacing=\"0\">";
  table += "<tr><td>" + img + "</td></tr>";
  table += "<tr><td><b>Title: </b>" + title + "</td></tr>";
  table += "<tr><td><b>Description: </b>" + description + "</td></tr>";
  table += "<tr><td>";
  table += "<table align=\"center\"><tr>"
  + "<td>[<a href=\"javascript:hidePopUpDiv();\">close</a>]</td>"
  + "</tr></table>";
  table += "</td></tr>";
  table += "</table>";
  document.getElementById("popUpDiv").innerHTML = table;
    
  document.getElementById("popUpDiv").style.visibility = "visible";
    
}

function hidePopUpDiv(){	
  document.getElementById("popUpDiv").style.visibility = "hidden";
}

function showAddress(response) {	
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("May be google doesn't know this location!");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml('<br><b>Address: </b>' + place.address);
  }
}

function trim(str, chars) {	
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {	
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {	
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkEnter(e){	
  var keynum;
  if(window.event){
    // IE
    keynum = e.keyCode;
  }
  else if(e.which){
    // Netscape/Firefox/Opera
    keynum = e.which;
  }
  if(keynum == 13){
    flyToLocation();
  }
}

function flyToLocation(){
  var flyToLocation = document.getElementById("inputLocation").value;
  map.clearOverlays();
  if (geocoder) {
    geocoder.getLatLng(
      flyToLocation,
      function(point) {
        if (!point) {
          alert(flyToLocation + " not found");
        } else {
          map.setCenter(point);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml('<br><b>Location: </b>' + flyToLocation);
          var lat = point.lat();
          var lon = point.lng();
          getImages(lat, lon);
        }
      }
      );
  }
}

